#include "OLInfoPanel.h"
#include "OLAppTemplateDefs.h"
#include "OLApplication.h"

#include <QtGui/QPainter>
#include <QtGui/QVBoxLayout>
#include <QtCore/QDebug>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
#include <QTime>
#include <QDate>
#include <QSystemInfo>
#include <QSystemNetworkInfo>
#include <QSystemDeviceInfo>
#include <QDesktopWidget>

#define TIME "__time"
#define DAY "__day"
#define DATE "__date"

QTM_USE_NAMESPACE

OLInfoPanel::OLInfoPanel(QWidget *parent) :
    QWidget(parent), m_opacity(1.0)
{
    QLabel* time = new QLabel(QTime::currentTime().toString("h:mm ap"), this);
    QFont font = time->font();
    font.setPixelSize(FONT_SIZE*2);
    time->setFont(font);
    time->setObjectName(TIME);

    QLabel* day = new QLabel(QDate::currentDate().toString("dddd"), this);
    font.setPixelSize(FONT_SIZE);
    day->setFont(font);
    day->setObjectName(DAY);

    QLabel* date = new QLabel(QDate::currentDate().toString("d MMMM yyyy"), this);
    font.setPixelSize(FONT_SIZE);
    date->setFont(font);
    date->setObjectName(DATE);

    startTimer(60000);

    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->setMargin(10);
    layout->setSpacing(0);

    layout->addWidget(time);
    layout->addWidget(day);
    layout->addWidget(date);

    layout->addSpacing(10);
    QSystemNetworkInfo* netInfo = new QSystemNetworkInfo(this);
    date->setFont(font);
    QSystemNetworkInfo::NetworkMode currentMode = netInfo->currentMode();
    QLabel* lbl = new QLabel(netInfo->networkName(currentMode));
    lbl->setFont(font);
    layout->addWidget(lbl);

    font = this->font();
    font.setPixelSize(FONT_SIZE-4);
    this->setFont(font);

    layout->addSpacing(20);
    layout->addWidget(new QLabel("App Version: " + OLApp->applicationVersion(), this));

#ifdef DEBUG_BUILD
    QSystemInfo* sysInfo = new QSystemInfo(this);
    layout->addWidget(new QLabel("OS: " + sysInfo->version(QSystemInfo::Os), this));
    layout->addWidget(new QLabel("Qt Core: " + sysInfo->version(QSystemInfo::QtCore), this));
    layout->addWidget(new QLabel("Qt Mobility: " + sysInfo->version(QSystemInfo::QtMobility), this));

    QSystemDeviceInfo devInfo;
    QByteArray userAgentBuffer;
    userAgentBuffer.reserve(512);
    userAgentBuffer.append(devInfo.manufacturer());
    userAgentBuffer.append("; ");
    userAgentBuffer.append(devInfo.model());
    userAgentBuffer.append("; ");
    userAgentBuffer.append(sysInfo->version(QSystemInfo::Os));
    userAgentBuffer.append("; ");
    QRect screenSize = qApp->desktop()->screenGeometry();
    userAgentBuffer.append(QString::number(screenSize.width()));
    userAgentBuffer.append('x');
    userAgentBuffer.append(QString::number(screenSize.height()));
    layout->addSpacing(20);
    layout->addWidget(new QLabel("Device: " + userAgentBuffer, this));
#endif

    setLayout(layout);
}

void OLInfoPanel::timerEvent(QTimerEvent *event)
{
    QLabel* lbl = findChild<QLabel*>(TIME);
    if(lbl) {
        lbl->setText(QTime::currentTime().toString("h:mm ap"));
    }

    lbl = findChild<QLabel*>(DAY);
    if(lbl) {
        lbl->setText(QDate::currentDate().toString("dddd"));
    }

    lbl = findChild<QLabel*>(DATE);
    if(lbl) {
        lbl->setText(QDate::currentDate().toString("d MMMM yyyy"));
    }

}

void OLInfoPanel::paintEvent(QPaintEvent*)
{
    QPainter p(this);

    p.setOpacity(m_opacity);

    p.setBrush(Qt::NoBrush);
    p.setPen(OLStyles::buttonHighlightBorder());

    p.drawLine(0, height()-1, width(), height()-1);

/*
    QPainter p(this);

    p.setOpacity(m_opacity);

    // draw background
    p.setPen(Qt::white);
    p.setBrush(QColor(0,0,0,150));
    p.drawRoundedRect(rect().adjusted(0,0,-1,-1), 5, 5);

    // work out if a gridlayout is used or not
    bool isLandscape = false;
    QGridLayout* currentLayout = qobject_cast<QGridLayout*>(layout());
    if(currentLayout!=0)
        isLandscape = true;

    // draw separators between widgets
    p.setPen(QColor(99,99,99));
//    QList<OLButton*> widgets = findChildren<OLButton*>();
//    int childCount = widgets.count();
    int childCount = 0;

    int bottomLineOffset = isLandscape ? (childCount+1)/2 : childCount;
    --bottomLineOffset;
    for(int ii=0; ii<bottomLineOffset; ++ii) {
//        OLButton* btn = widgets[ii];
//        if( btn->isVisible() ) {
//            int ypos = widgets[ii]->geometry().bottom() + 5;
//            p.drawLine(10, ypos, width()-10, ypos);
//        }
    }

    if(isLandscape) {
        // draw separator thru middle
        p.drawLine(width()/2, 10, width()/2, height()-10);
    }
*/
}

void OLInfoPanel::setOpacity(qreal opacity)
{
    m_opacity = opacity;
    update();
}

qreal OLInfoPanel::opacity()
{
    return m_opacity;
}

void OLInfoPanel::handlePortrait()
{
//    QList<QWidget*> panelItems = findChildren<QWidget*>();
//    delete layout();

//    QVBoxLayout* newLayout = new QVBoxLayout(this);
//    newLayout->setContentsMargins(10,10,10,10);
//    newLayout->setSpacing(10);

//    int btnCount = panelItems.count();
//    for(int ii=0; ii<btnCount; ++ii) {
//        newLayout->addWidget(panelItems[ii]);
//    }

//    setLayout(newLayout);
//    resize(width(), minimumSizeHint().height());
}

void OLInfoPanel::handleLandscape()
{
//    QList<QWidget*> panelItems = findChildren<QWidget*>();
//    delete layout();

//    QHBoxLayout* newLayout = new QHBoxLayout(this);
//    newLayout->setContentsMargins(10,10,10,10);
//    newLayout->setSpacing(10);

//    int btnCount = panelItems.count();
//    for(int ii=0; ii<btnCount; ++ii) {
//        newLayout->addWidget(panelItems[ii]);
//    }

//    setLayout(newLayout);
//    resize(width(), minimumSizeHint().height());
}


